<?php

namespace app\controller;

use app\BaseController;
use app\model\Archives;
use app\model\Character;
use app\model\Collection;
use app\model\Contract;
use app\model\Parameter;
use app\model\User_character;

/**
 * 业务收款
 * Class Collection
 * @package app\controller
 * @author 下雨吧จุ๊บ
 */
class CollectionController extends BaseController
{
    /**
     * @apiGroup 业务收款
     * @apiVersion 1.0.0
     * @api {GET} /collection  业务收款列表
     * @apiDescription 业务收款列表
     * @apiParam {String} listRows 每页数量(必填*)
     * @apiParam {String} page 当前页数(必填*)
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * data: 业务收款列表,
     * message: '获取业务收款列表成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function index()
    {
        $data = input();
        if (empty($data['listRows'])) {
            $data['listRows'] = '8';
        }
        if (empty($data['page'])) {
            $data['page'] = '1';
        }
        $map[] = ['a.title','like','%%'];
        if (!empty($data['map']['title'])){
            $map[] = ['a.title','like','%'. $data['map']['title'] .'%'];
        }
        if (empty($data['archivesid'])){
            //左侧菜单业务收款按钮
            $data['list'] = Collection::alias('col')
                ->join(['contract' => 'con'], 'con.id=col.contractid')
                ->join(['user' => 'u'], 'u.id=col.userid')
                ->join(['archives' => 'a'], 'a.id=col.archivesid')
                ->join(['parameter' => 'p'], 'p.id=col.paytypeid')
                ->where($map)
                ->field('col.*,u.realname,p.title as parametertitle,con.code,con.customer,con.contractprice,con.saleprice,a.title as archivestitle')
                ->paginate([
                    'list_rows' => $data['listRows'],
                    'page' => $data['page'],
                ]);
        }else{
            //客户档案中业务收款按钮
            $data['list'] = Collection::alias('col')
                ->join(['contract' => 'con'], 'con.id=col.contractid')
                ->join(['user' => 'u'], 'u.id=col.userid')
                ->join(['parameter' => 'p'], 'p.id=col.paytypeid')
                ->where('col.archivesid',$data['archivesid'])
                ->field('col.*,u.realname,p.title as parametertitle,con.code,con.customer,con.saleprice,con.contractprice')
                ->paginate([
                    'list_rows' => $data['listRows'],
                    'page' => $data['page'],
                ]);
        }
        success($data, '获取业务收款列表成功');
    }

    /**
     * @apiGroup 业务收款
     * @apiVersion 1.0.0
     * @api {GET} /collection/create  新增
     * @apiDescription 新增界面
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * data: 跟进阶段列表,
     * message: '获取数据成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function create()
    {
        $data = input();
        //客户档案列表
        $data['archives'] = Archives::select();
        //获取合同编号
        $data['code'] = Contract::where('status',1)->select();
        if (!empty($data['archivesid'])){
            $data['code'] = Contract::where('status',1)->where('archivesid',$data['archivesid'])->select();
        }
        //部门列表
        $data['character'] = Character::where('status', 1)->select();
        //获取支付方式列表
        $data['paytype'] = Parameter::where('status', 1)->where('pid', 82)->select();
        success($data, '获取数据成功');
    }

    /**
     * @apiGroup 业务收款
     * @apiVersion 1.0.0
     * @api {POST} /collection  新增保存
     * @apiDescription 新增保存
     * @apiParam {String} archivesid 客户档案id
     * @apiParam {String} contractid 合同id(必填*)
     * @apiParam {String} customer 客户名称(必填*)
     * @apiParam {String} userid 收款人id(必填*)
     * @apiParam {String} paytypeid 支付方式id(必填*)
     * @apiParam {String} collectionprice 收款金额(必填*)
     * @apiParam {String} collectiontime 收款日期(必填*)
     * @apiParam {String} content 备注
     * @apiParam {String} status 状态
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * message: '新增成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function save()
    {
        $data = input();
        //必填项验证
        if (empty($data['archivesid'])) {
            error('请选择客户');
        }
        //必填项验证
        if (empty($data['contractid'])) {
            error('请选择合同编号');
        }
        //必填项验证
        if (empty($data['paytypeid'])) {
            error('请选择付款方式');
        }
        //必填项验证
        if (empty($data['userid'])) {
            error('请选择收款人');
        }
        //补充时间
        $data['createtime'] = time();
        $data['updatetime'] = time();
        //添加数据
        $result = Collection::insert($data);
        if ($result) {
            success(array(), '新增成功');
        }
        error('新增失败');
    }

    /**
     * @apiGroup 业务收款
     * @apiVersion 1.0.0
     * @api {GET} /collection/:id  编辑读取
     * @apiDescription 编辑页面(:id是指当前id,直接写在路由中)
     * @apiParam {String} id id
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * data: array
     * message: '获取数据成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function read()
    {
        $data = input();
        //必填项验证
        if (empty($data['id'])) {
            error('520数据错误(Missing parameter id)');
        }
        //客户档案列表
        $data['archives'] = Archives::select();
        //部门列表
        $data['character'] = Character::where('status', 1)->select();
        //获取支付方式列表
        $data['paytype'] = Parameter::where('status', 1)->where('pid', 82)->select();
        //返回数据详情
        $data['info'] = Collection::where('id', $data['id'])->find();
        //获取业务人员列表
        $userids = User_character::where('characterid',$data['info']['characterid'])->select();
        $data['user'] = array();
        if (!empty($userids)){
            foreach ($userids as $v){
                $data['user'][] = \app\model\User::where('id', $v['userid'])->find();
            }
        }
        //获取合同编号
        $data['code'] = Contract::alias('co')
            ->join(['character' => 'c'], 'c.id=co.characterid')
            ->join(['parameter' => 'p'], 'p.id=co.paytypeid')
            ->join(['parameter' => 'p1'], 'p1.id=co.contracttypeid')
            ->join(['user' => 'u'], 'u.id=co.userid')
            ->join(['user' => 'u1'], 'u1.id=co.serviceid')
            ->where('archivesid',$data['info']['archivesid'])
            ->field('co.*,u.realname as name,u1.realname as servicename,c.title as charactertitle,p.title as paytypetitle,p1.title as contracttypetitle')
            ->select();
        //客户名称
        $data['info']['archivestitle'] = Archives::where('id',$data['info']['archivesid'])->find()['title'];
        //合同号码
        $data['info']['code'] = Contract::where('id',$data['info']['contractid'])->find()['code'];
        success($data, '获取数据成功');
    }

    /**
     * @apiGroup 业务收款
     * @apiVersion 1.0.0
     * @api {PUT} /collection/:id  编辑保存
     * @apiDescription 编辑页面保存(:id是指当前id,直接写在路由中)
     * @apiParam {String} contractid 合同id(必填*)
     * @apiParam {String} customer 客户名称(必填*)
     * @apiParam {String} userid 收款人id(必填*)
     * @apiParam {String} paytypeid 支付方式id(必填*)
     * @apiParam {String} collectionprice 收款金额(必填*)
     * @apiParam {String} collectiontime 收款日期(必填*)
     * @apiParam {String} content 备注
     * @apiParam {String} status 状态
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * message: '保存成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function update()
    {
        $data = input();
        //必填项验证
        if (empty($data['id'])) {
            error('请选择客户');
        }
        //必填项验证
        if (empty($data['contractid'])) {
            error('请选择合同编号');
        }
        //必填项验证
        if (empty($data['paytypeid'])) {
            error('请选择付款方式');
        }
        //必填项验证
        if (empty($data['userid'])) {
            error('请选择收款人');
        }
        //更新修改时间
        $data['updatetime'] = time();
        //更新数据
        $result = Collection::where('id', $data['id'])->save($data);
        success(array(), '保存成功');
    }

    /**
     * @apiGroup 业务收款
     * @apiVersion 1.0.0
     * @api {DELETE} /collection/0  删除
     * @apiDescription 删除(路由中直接写0,ids集合使用参数传递)
     * @apiParam {array} ids id集合
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * message: '删除成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function delete()
    {
        $data = input();
        //必填项验证
        if (empty($data['ids'])) {
            error('520数据错误(Missing parameter id)');
        }
        $result = Collection::whereIn('id', $data['ids'])->delete();
        success(array(), '删除成功');
    }

    /**
     * @apiGroup 业务收款
     * @apiVersion 1.0.0
     * @api {GET} /collection/:id/edit  选择合同编号请求
     * @apiDescription 读取数据(:id是指当前id,直接写在路由中)
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * message: '获取数据成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function edit()
    {
        $data = input();
        //必填项验证
        if (empty($data['id'])) {
            error('520数据错误(Missing parameter id)');
        }
        //根据合同id获取合同详情
        $data['info'] = Contract::where('id',$data['id'])->find();
        $data['info']['remainingprice'] = $data['info']['saleprice'] - Collection::where('contractid',$data['id'])->sum('collectionprice');
        success($data,'获取数据成功');
    }
}
